import java.util.Arrays;

class Solution {
    //选择
    public int[] sortArray1(int[] nums) {
        for (int i = 0; i < nums.length-1; i++) {
            int minIndex = i;
            for (int j = i; j < nums.length; j++) {
                if (nums[j] < nums[minIndex]) {
                    minIndex = j;
                }
            }
            swap(nums,minIndex,i);
        }
        return nums;
    }

    //选择优化
    public int[] sortArray3(int[] nums) {
        int left = 0;
        int right = nums.length-1;
        while (left < right) {
            int minIndex = left;
            int maxIndex = left;
            for (int i = left; i <= right; i++) {
                if (nums[i] > nums[maxIndex]) {
                    maxIndex = i;
                }
                if (nums[i] < nums[minIndex]) {
                    minIndex = i;
                }
            }
            swap(nums,minIndex,left);
            if (maxIndex == left) {
                maxIndex = minIndex;
            }
            swap(nums,maxIndex,right);
            left++;
            right--;
        }
        return nums;
    }

    public int[] sortArray2(int[] nums) {
        int gap = nums.length;
        while (gap != 1) {
            gap /= 2;
            shellSort(nums,gap);
        }
        return nums;
    }
    public void shellSort(int[] nums,int gap) {
        for (int i = gap; i < nums.length; i++) {
            int tmp = nums[i];
            int j = i-gap;
            for (; j >= 0; j -= gap) {
                if (tmp < nums[j]) {
                    nums[j+gap] = nums[j];
                }else {
                    nums[j+gap] = tmp;
                    break;
                }
            }
            nums[j+gap] = tmp;
        }
    }


    //堆排序
    public int[] sortArray4(int[] nums) {
        creatHeap(nums);
        heapSort(nums);
        return nums;
    }

    private void heapSort(int[] nums) {
        int end = nums.length;
        while (end > 0) {
            swap(nums,end-1,0);
            siftDown(nums,0,end-1);
            end--;
        }
    }

    private void creatHeap(int[] nums) {
        int parent = (nums.length-1-1)/2;
        while(parent >= 0) {
            siftDown(nums,parent,nums.length);
            parent--;
        }
    }

    private void siftDown(int[] nums, int parent,int length) {
        int child = parent*2+1;
        while (child < length) {
            if (child+1 < length) {
                if (nums[child] < nums[child+1]) {
                    child += 1;
                }
            }
            if (nums[parent] < nums[child]) {
                swap(nums,parent,child);
                parent = child;
                child = parent*2+1;
            }else {
                break;
            }
        }
    }

    public int[] sortArray5(int[] nums) {
        quickSort(nums);
        return nums;
    }

    private void quickSort(int[] nums) {
        quick(nums,0,nums.length-1);
    }

    private void quick(int[] nums, int start, int end) {
        if (end-start+1 <= 7) {
            insertRange(nums,start,end);
            return;
        }

        int midIndex = getMiddle(nums,start,end);
        swap(nums,midIndex,start);

        int pivot = partition(nums,start,end);

        quick(nums,start,pivot-1);
        quick(nums,pivot+1,end);
    }

    private void insertRange(int[] nums,int start,int end) {
        for (int i = start+1; i <= end; i++) {
            int tmp = nums[i];
            int j = i-1;
            for (; j >= start; j--) {
                if (tmp < nums[j]) {
                    nums[j+1] = nums[j];
                }else {
                    nums[j+1] = tmp;
                    break;
                }
            }
            nums[j+1] = tmp;
        }
    }

    private int getMiddle(int[] nums, int start, int end) {
        int mid = (start+end) / 2;
        if (nums[start] > nums[end]) {
            if (nums[mid] > nums[start]) {
                return start;
            }else if (nums[mid] < nums[end]) {
                return end;
            }else {
                return mid;
            }
        }else {
            if (nums[mid] > nums[end]) {
                return end;
            } else if (nums[mid] < nums[start]) {
                return start;
            }else {
                return mid;
            }
        }
    }

    private int partition(int[] nums, int left, int right) {
        int tmp = nums[left];
        while (left < right) {
            while (left < right && nums[right] >= tmp) {
                right--;
            }
            nums[left] = nums[right];

            while (left < right && nums[left] <= tmp) {
                left++;
            }
            nums[right] = nums[left];
        }
        nums[left] = tmp;
        return left;
    }

    private void swap(int[] nums, int minIndex, int i) {
        int tmp = nums[minIndex];
        nums[minIndex] = nums[i];
        nums[i] = tmp;
    }

    public int[] sortArray(int[] nums) {
        mergeSort(nums,0,nums.length-1);
        return nums;
    }

    private void mergeSort(int[] nums, int left, int right) {
        if (left >= right) {
            return;
        }
        int mid = (left+right)/2;
        mergeSort(nums,left,mid);
        mergeSort(nums,mid+1,right);

        merge(nums,left,right,mid);
    }
    private void merge(int[] nums, int left, int right, int mid) {
        int s1 = left;
        int e1 = mid;
        int s2 = mid+1;
        int e2 = right;
        int k = 0;
        int[] tmp = new int[right-left+1];

        while (s1 <= e1 && s2 <= e2) {
            if (nums[s1] <= nums[s2]) {
                tmp[k++] = nums[s1++];
            }else {
                tmp[k++] = nums[s2++];
            }
        }

        while (s1 <= e1) {
            tmp[k++] = nums[s1++];
        }
        while (s2 <= e2) {
            tmp[k++] = nums[s2++];
        }

        for (int i = 0; i < tmp.length; i++) {
            nums[i+left] = tmp[i];
        }
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] arr = {234,54,12,2,54,12,23,11,0};
        solution.sortArray(arr);
        System.out.println(Arrays.toString(arr));
    }
}